package com.web.servlet.manager;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



import com.web.servlet.config.Config;
import com.web.servlet.utils.RedisHelper;

public class RedisManager extends BaseManager {

	public static final Logger log = LoggerFactory
			.getLogger(RedisManager.class);
	public static final int redisSize = 10;

	private static RedisHelper redisHelper = null;

	// 异步即时存储队列，填入即刻存储
	//private static LinkedBlockingQueue<DataObject> savequeue = new LinkedBlockingQueue<DataObject>();

	Thread sendThread = null;
	private static RedisManager redisManager;

	public static RedisHelper getRedis() {
		return redisHelper;
	}

	/*public static boolean addQueue(DataObject db) {
		return savequeue.offer(db);
	}*/

	/**
	 * 获得RedisManager单体类
	 * 
	 * @return
	 */
	public static synchronized RedisManager getInstance() {
		if (redisManager == null) {
			redisManager = new RedisManager();
		}
		return redisManager;
	}

	/*protected void startSendThread() {
		sendThread = new Thread("saveReidsThread") {
			public void run() {
				long timeout = 1000 * 10;
				DataObject data = null;
				while (true) {
					try {
						// 如果sbm为null，则从队列读取，否则继续发送sbm
						data = savequeue.poll(timeout, TimeUnit.MILLISECONDS);
						if (data == null) {
							continue;
						}
						data.save();
					} catch (Exception e) {
						log.error(Util.getExceptionStack(e));
					}
				}
			}
		};
		sendThread.start();
	}*/

	@Override
	public void init() {
		// TODO Auto-generated method stub
		redisHelper = new RedisHelper(Config.redisConfigMap.get("url"),
				Integer.valueOf(Config.redisConfigMap.get("port")), redisSize,
				Config.redisConfigMap.get("password"));
		//startSendThread();
	}

}
